/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.web.client.ui;

import at.socialconference.inter.connection.protocol.SocketProtocol;
import at.socialconference.web.client.SCwebService;
import at.socialconference.web.client.SCwebService.Util;
import at.socialconference.web.client.util.AsyncCallbackGetClientUser;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Provides a login form
 * 
 */
public class Login extends Composite {

	private class LoginCallback implements AsyncCallback<Integer> {
		public void onSuccess(Integer result) {
			if (result == 1)
				Util.getInstance().getClientUser(new AsyncCallbackGetClientUser());
			else if (result == SocketProtocol.RESP_NO_LOGIN)
				Window.alert("Wrong username");
			else if (result == SocketProtocol.RESP_WRONG_PW)
				Window.alert("Wrong password");
			else if (result == SocketProtocol.RESP_SESSION_CREATION_FAILED)
				Window.alert("Session creation failed");
			else if (result == -1)
				Window.alert("Internal server error: Backend unreachable");
			else
				Window.alert("Internal server error...");
		}

		public void onFailure(Throwable caught) {
			Window.alert("Server error: Unable to reach server");
		}
	}

	// TextBox for the User Name
	private TextBox txtLogin = new TextBox();
	// PasswordTextBox for the password
	private PasswordTextBox txtPassword = new PasswordTextBox();
	/** Button Login for the login */
	private Button btnLogin = new Button("login");

	private FlowPanel content = new FlowPanel();

	public Login() {
		Label lblLogin = new Label("Please login to get full functionality");
		content.setStyleName("login");

		content.add(lblLogin);
		Label label = new Label("Username");
		content.add(label);
		content.add(txtLogin);
		content.add(new Label("Password"));
		content.add(txtPassword);
		btnLogin.setStyleName("login");
		content.add(btnLogin);

		txtPassword.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				char keyCode = event.getCharCode();
				if (keyCode == KeyCodes.KEY_ENTER) {
					SCwebService.Util.getInstance().login(txtLogin.getText(), txtPassword.getText(), new LoginCallback());
				}
			}
		});

		btnLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent Klikka) {
				SCwebService.Util.getInstance().login(txtLogin.getText(), txtPassword.getText(), new LoginCallback());
			};
		});

		initWidget(content);
	}
}
